<?php
namespace Chat\Model;
use Think\Model;
class ChatContributModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_chat_contribut'; 
	
	
	public function getContributList($data){
		$map = array();
		if(!empty($data['chatid'])){
			$map['chatid'] = $data['chatid'];
		}
		
		$total = $this->where($map)->count();
		$fields = array('contributid','chatid','custid','amount','content','totalamount','type','createtime');
		
		if(empty($data['sortField']))$data['sortField'] = "contributid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	public function addTotalamount($contributid , $amount){
		$contributid = (int)$contributid;
		$amount = number_format($amount, 2, '.', '');
		
		$sql = "update qyx_chat_contribut set totalamount=totalamount+{$amount} where contributid={$contributid}";
		$this->execute($sql);
	}
	
	
	public function getContributById($contributid){
		$map['contributid'] = $contributid;
		$result = $this->where($map)->find();
		return $result;
	}
	
	public function addContribut($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function updateContributByChatid($data){
		$map['chatid'] = $data['chatid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function changeAmount($data){
		$map['chatid'] = $data['chatid'];
		$map['type'] = 1;
		$result = $this->where($map)->save($data);
		return $result;
	}
}


